package com.linran.structure_algorithm.算法.lesson2_排序;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * 选择排序
 */
public class TwoSelectorSort {

    public static void main(String[] args) {
//        baseTest();
        test80000();
    }

    public static void baseTest() {
        int[] arr = {101, 99, 54, 1,234,5,-1,4};
        selectorSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void test80000() {
        int[] arr = new int[80000];
        for (int i = 0; i < 80000 - 1; i++) {
            arr[i] = (int)(Math.random()*80000);
        }
        LocalDateTime start = LocalDateTime.now();
        selectorSort(arr);
        LocalDateTime end = LocalDateTime.now();
        System.out.println("排序后的数组");
        System.out.println(Arrays.toString(arr));
        System.out.printf("耗时:%d毫秒%n", Duration.between(start, end).toMillis());
    }

    public static void selectorSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            int min = arr[i];
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < min) {
                    min = arr[j];
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                arr[minIndex] = arr[i];
                arr[i] = min;
            }
        }
    }
}
